package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.monitor.domain.*;
import com.pds.web.monitor.dto.ContractDTO;
import com.pds.web.monitor.mapper.*;
import com.pds.web.monitor.service.IPdsMonitorContractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("pdsMonitorContractService")
public class PdsMonitorContractServiceImpl extends ServiceImpl<PdsMonitorContractMapper, PdsMonitorContract> implements IPdsMonitorContractService {

    @Resource
    private PdsMonitorContractMapper pdsMonitorContractMapper;

    @Resource
    private PdsMonitorTaskMapper pdsMonitorTaskMapper;

//    @Resource
//    private PdsMonitorTaskStepMapper pdsMonitorTaskStepMapper;

    @Resource
    private PdsMonitorTaskSamplingMapper pdsMonitorTaskSamplingMapper;

    @Resource
    private PdsMonitorTaskAnalysisMapper pdsMonitorTaskAnalysisMapper;

    @Resource
    private PdsMonitorTaskReportMapper pdsMonitorTaskReportMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsMonitorContract insertContract(PdsMonitorContract pdsMonitorContract) {
//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
//        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Long organizationId = pdsMonitorContractMapper.selectOrganizationId(233L);
        Date createTime = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorContract.setCreateTime(createTime);
        pdsMonitorContract.setStatus("1");
//        pdsMonitorContract.setUserId(2L);
        pdsMonitorContract.setOrganizationId(organizationId);
        pdsMonitorContractMapper.insertContract(pdsMonitorContract);
        return pdsMonitorContract;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updateContract(PdsMonitorContract pdsMonitorContract) {
        Date updateTime = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorContract.setUpdateTime(updateTime);
        return pdsMonitorContractMapper.updateContract(pdsMonitorContract) > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deleteContract(Long id) {
        QueryWrapper<PdsMonitorTask> qw1 = new QueryWrapper<PdsMonitorTask>();
        List<PdsMonitorTask> pdsMonitorTaskList = pdsMonitorTaskMapper.selectList(qw1.eq("contract_id", id));
        QueryWrapper<PdsMonitorTask> qw2 = new QueryWrapper<PdsMonitorTask>();
        boolean flag1 = pdsMonitorTaskMapper.delete(qw2.eq("contract_id", id)) > 0;
//        QueryWrapper<PdsMonitorTaskStep> qw3 = new QueryWrapper<PdsMonitorTaskStep>();
//        boolean flag2 = pdsMonitorTaskStepMapper.delete(qw3.eq("task_id", pdsMonitorTask.getId())) > 0;
        pdsMonitorTaskList.stream().forEach(pdsMonitorTask -> {
            QueryWrapper<PdsMonitorTaskSampling> qw3 = new QueryWrapper<>();
            pdsMonitorTaskSamplingMapper.delete(qw3.eq("task_id", pdsMonitorTask.getId()));
            QueryWrapper<PdsMonitorTaskAnalysis> qw4 = new QueryWrapper<>();
            pdsMonitorTaskAnalysisMapper.delete(qw4.eq("task_id", pdsMonitorTask.getId()));
            QueryWrapper<PdsMonitorTaskReport> qw5 = new QueryWrapper<>();
            pdsMonitorTaskReportMapper.delete(qw5.eq("task_id", pdsMonitorTask.getId()));
        });

        boolean flag5 = pdsMonitorContractMapper.deleteContract(id) > 0;
        return flag1 & flag5;
    }

    @Override
    public List<PdsMonitorContractVO> listCompanyContract(MonitorContractDTO monitorContractDTO) {
        List<PdsMonitorContractVO> pdsMonitorContractVOList = pdsMonitorContractMapper.listContract(monitorContractDTO);
//        pdsMonitorContractVOList.stream().forEach(pdsMonitorContractVO -> {
//            List<CompanyContact> companyContactList = pdsMonitorContractMapper.selectContact(pdsMonitorContractVO.getCompanyId());
//            CompanyContact companyContact = companyContactList.get(0);
//            pdsMonitorContractVO.setCompanyContact(companyContact);
//        });
        return pdsMonitorContractVOList;
    }

    @Override
    public List<PdsMonitorContractVO> listOrganizationContract(MonitorContractDTO monitorContractDTO) {
        List<PdsMonitorContractVO> pdsMonitorContractVOList = pdsMonitorContractMapper.listContract(monitorContractDTO);
//        pdsMonitorContractVOList.stream().forEach(pdsMonitorContractVO -> {
//            List<CompanyContact> companyContactList = pdsMonitorContractMapper.selectContact(pdsMonitorContractVO.getCompanyId());
//            CompanyContact companyContact = companyContactList.get(0);
//            pdsMonitorContractVO.setCompanyContact(companyContact);
//        });
        return pdsMonitorContractVOList;
    }

    @Override
    public List<PdsMonitorContractVO> listAll(ContractDTO contractDTO) {
        return pdsMonitorContractMapper.listAll(contractDTO);
    }

    @Override
    public PdsMonitorContractVO queryById(Long id) {
        return pdsMonitorContractMapper.queryById(id);
    }

    @Override
    public Long getCompanyId(long deptId) {
        return pdsMonitorContractMapper.selectCompanyId(deptId);
    }

    @Override
    public Long getOrganizationId(long deptId) {
        return pdsMonitorContractMapper.selectOrganizationId(deptId);
    }


}
